﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.CSharp;

namespace CLIGenConsole
{
    class Program
    {
        static bool ReadCommand(out string[] command)
        {
            string line = Console.ReadLine();
            if (!line.Contains(' '))
                command = new string[] { line };
            else
            {
                List<string> cmd = new List<string>();
                bool inQuotes = false;
                int paramIndex = 0;
                foreach (char c in line)
                {
                    switch (c)
                    {
                        case '"':
                            inQuotes = !inQuotes;
                            break;
                        case ' ':
                            if (inQuotes)
                                cmd[paramIndex] += c;
                            else
                                paramIndex++;
                            break;
                        default:
                            cmd[paramIndex] += c;
                            break;
                    }
                }

                if (!inQuotes)
                    command = cmd.ToArray();
                else
                    throw new ArgumentException("Quote mismatch.");
            }

            if (command[0] == "exit")
                return false;
            else if (command[0] == "quit")
                return false;
            return true;
        }

        static bool Prompt(string msg)
        {
            Console.WriteLine(msg);
            ConsoleKeyInfo key = Console.ReadKey(true);
            while (true)
            {
                if (key.KeyChar == 'y' || key.KeyChar == 'Y')
                    return true;
                else if (key.KeyChar == 'n' || key.KeyChar == 'N')
                    return false;
            }
        }

        static bool PromptSave()
        {
            return Prompt("Do you want to save the current CLI? y/n");            
        }

        static void Main(string[] args)
        {
            CLI CurrentCLI = null;
            string[] command;

            while (ReadCommand(out command))
            {
                switch (command[0])
                {
                    case "new":
                        if (CurrentCLI != null)
                        {
                            CurrentCLI.Save(CurrentCLI.Name + ".xml");
                            CurrentCLI = new CLI();
                        }
                        else if (command.Length == 1)
                        {
                            CurrentCLI = new CLI();
                        }
                        else
                        {
                            CurrentCLI = new CLI();
                        }
                        break;
                    case "save":
                        if (command.Length == 1)
                            CurrentCLI.Save(CurrentCLI.Name + ".xml");
                        else
                            CurrentCLI.Save(command[1]);
                        break;
                    case "print":
                        if (command.Length == 1)
                            Console.WriteLine(CurrentCLI.Print());
                        else
                            CurrentCLI.Print(command[1]);
                        break;
                    case "compile":
                        if (command.Length == 1)
                            CurrentCLI.Compile();
                        else
                            CurrentCLI.Compile(command[1]);
                        break;
                    case "add":
                        if (command.Length != 3)
                            Console.WriteLine("Parameter(s) missing.");
                        else
                            CurrentCLI.AddCommand(command[1], command[2]);
                        break;
                    default:
                        Console.WriteLine("Command not recognized.");
                        break;
                }
            }
        }
    }
}
